var ctx;
var configLabel;
var pSize;

function setCtx(ctx){
	this.ctx = ctx;
}

//判断是否为空
function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	return mess;
}

//条件搜索用户列表
function getIntegralConfigList(){
	configLabel = ifNull($("#configLabel").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(configLabel == $("#configLabel").attr("placeholder")){
		configLabel = "";
	}
	loadContent("/integralConfig/integralConfigList?pNo=1&configLabel="+ configLabel + "&pSize="+ pSize);
	
}

//跳转到指定页面
function gotoIntegralConfigPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	configLabel = ifNull($("#configLabel").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(configLabel == $("#configLabel").attr("placeholder")){
		configLabel = "";
	}
	loadContent("/integralConfig/integralConfigList?pNo="+ page +"&configLabel="+ configLabel + "&pSize="+ pSize);
}

//跳转到上一页或者下一页
function gotoPage(page){
	configLabel = ifNull($("#configLabel").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(configLabel == $("#configLabel").attr("placeholder")){
		configLabel = "";
	}
	loadContent("/integralConfig/integralConfigList?pNo="+ page +"&configLabel="+ configLabel + "&pSize="+ pSize);
}

//所有业主积分清零
function clearAll(){
	var message = "确定将全部业主积分清零？";
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/integralConfig/integralAllClear",
			cache : false,
			dataType : 'json',
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/integralConfig/integralConfigList");
					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}

//启用或禁用配置
function approved(id, state) {
	var message;
	switch (parseInt(state)) {
	case 1:
		message = "是否禁用？";
		break;
	case 0:
		message = "是否启用？";
		break;
	default:
		break;
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/integralConfig/integralConfigUpdate",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				state : state
			},
			success : function(result) {
				if (result.success) {
					// alert("操作成功");

					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						if (state == 1) {
							loadContent("/integralConfig/integralConfigList");
						} else {
							loadContent("/integralConfig/integralConfigList");
						}

					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}

//新增或更新便民黄页(1:新增 2:编辑)
function saveOrUpdateConfig(configId) {
	if (configId != null) {
		msg = "编辑积分分类";
		url = ctx + "/integralConfig/editIntegralConfig?id=" + configId + "";
		saveOrUpdateBox(msg, url);
	} else {
		msg = "新增积分分类";
		url = ctx + "/integralConfig/editIntegralConfig?id=0";
		saveOrUpdateBox(msg, url);
	}

}

//保存修改
var index;
function saveOrUpdateBox(msg,url){
  //官网欢迎页
   index = layer.open({
      type: 2,
      //skin: 'layui-layer-lan',
      title: msg,
      fix: false,
      shadeClose: true,
      scrollbar: false,
      maxmin: false,
      edit: true,
      closeBtn: 1,
      area: ['500px', '212px'],
      content: url,
      btn: ['确定', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调
	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.saveOrUpdateConfig();

	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	    });
}

//全选和反选
function selectAll(target){    
    if(target.checked){    
        $("#tableTbody :checkbox").attr("checked", true); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#tableTbody :checkbox").attr("checked", false); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","");
       });
    }    
}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

//选择或取消选中当前记录
function selectOne(id,target){
    if(target.checked){    
        $("#integralConfig" + id).attr("checked", true);   
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#integralConfig" + id).attr("checked", false); 
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","");
       });
    } 
}

//删除多个id对应的记录
function deleteByIds(){
	if($("img[name='integralConfigChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='integralConfigChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    var vals = delIds.join(',');//转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/integralConfig/deleteConfig",{
					iDs:vals
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							loadContent("/integralConfig/integralConfigList");
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});
	}
}
